package com.lineage.server.serverpackets;

import com.lineage.server.datatables.ShopCnTable;
import com.lineage.server.model.Instance.L1ItemStatus;
import com.lineage.server.model.Instance.L1NpcInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.templates.L1Item;
import com.lineage.server.templates.L1ShopItem;
import java.util.ArrayList;
import java.util.Iterator;

public class S_ShopSellListCn extends ServerBasePacket {
  private byte[] _byte;
  
  public S_ShopSellListCn(L1PcInstance pc, L1NpcInstance npc) {
    this._byte = null;
    buildPacket(pc, npc.getId(), npc.getNpcId());
  }
  
  private void buildPacket(L1PcInstance pc, int tgObjid, int npcid) {
    writeC(70);
    writeD(tgObjid);
    ArrayList<L1ShopItem> shopItems = ShopCnTable.get().get(npcid);
    if (shopItems.size() <= 0) {
      writeH(0);
      return;
    } 
    writeH(shopItems.size());
    int i = 0;
    Iterator<L1ShopItem> iterator = shopItems.iterator();
    while (iterator.hasNext()) {
      L1ShopItem shopItem = iterator.next();
      i++;
      pc.get_otherList().add_cnList(shopItem, i);
      L1Item item = shopItem.getItem();
      String nameString = "";
      if (shopItem.getEnchantLevel() > 1)
        nameString = "+" + shopItem.getEnchantLevel() + " "; 
      writeD(i);
      writeH(item.getGfxId());
      writeD(shopItem.getPrice());
      if (shopItem.getPackCount() > 1) {
        writeS(String.valueOf(String.valueOf(nameString)) + item.getNameId() + " (" + shopItem.getPackCount() + ")");
      } else {
        writeS(String.valueOf(String.valueOf(nameString)) + item.getNameId());
      } 
      L1ItemStatus itemInfo = new L1ItemStatus(item);
      byte[] status = itemInfo.getStatusBytes(true).getBytes();
      writeC(status.length);
      byte[] array;
      int length = (array = status).length;
      int j = 0;
      while (j < length) {
        byte b = array[j];
        writeC(b);
        j++;
      } 
    } 
    writeH(6100);
  }
  
  public S_ShopSellListCn(L1PcInstance pc, int npcId) {
    this._byte = null;
    buildPacket(pc, pc.getId(), npcId);
  }
  
  public byte[] getContent() {
    if (this._byte == null)
      this._byte = getBytes(); 
    return this._byte;
  }
  
  public String getType() {
    return getClass().getSimpleName();
  }
}
